package hsb.ruoyi.admin.web.controller.system;


import hsb.ruoyi.common.constant.Constants;
import hsb.ruoyi.common.constant.HttpStatus;
import hsb.ruoyi.common.core.domain.AjaxResult;
import hsb.ruoyi.common.core.domain.TokenService;
import hsb.ruoyi.common.core.domain.entity.SysMenu;
import hsb.ruoyi.common.core.domain.entity.SysUser;
import hsb.ruoyi.common.core.domain.model.LoginBody;
import hsb.ruoyi.common.core.domain.model.LoginUser;
import hsb.ruoyi.common.utils.SecurityUtils;
import hsb.ruoyi.framework.manager.AsyncManager;
import hsb.ruoyi.framework.manager.factory.AsyncFactory;
import hsb.ruoyi.framework.web.service.SysLoginService;
import hsb.ruoyi.framework.web.service.SysPermissionService;
import hsb.ruoyi.system.domain.vo.RouterVo;
import hsb.ruoyi.system.service.ISysMenuService;
import io.vertx.core.http.HttpServerRequest;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @POST
    @Path("/loginApi")
    @Consumes(MediaType.APPLICATION_JSON)
    public AjaxResult login(LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 登出方法
     *
     * @return 结果
     */
    @POST
    @Path("/logout")
    public AjaxResult logout(@Context HttpServerRequest request) {

        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            String userName = loginUser.getUserName();
            // 删除用户缓存记录
            tokenService.delLoginUser(loginUser.getToken());
            // 记录用户退出日志
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGOUT, "退出成功"));
        } catch (Exception e) {

        }

        return AjaxResult.error(HttpStatus.SUCCESS, "退出成功");
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GET
    @Path("getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GET
    @Path("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        List<RouterVo> routerVos = menuService.buildMenus(menus);
        return AjaxResult.success(routerVos);
    }
}
